package com.gitee.swsk33.lucenedemo.util;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.lionsoul.jcseg.ISegment;
import org.lionsoul.jcseg.analyzer.JcsegAnalyzer;
import org.lionsoul.jcseg.dic.DictionaryFactory;
import org.lionsoul.jcseg.segmenter.SegmenterConfig;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 分词器相关实用类
 */
public class AnalyzerUtils {

	/**
	 * 创建一个默认的Jcseg中文分词器
	 *
	 * @return 分词器对象
	 */
	public static Analyzer createDefaultAnalyzer() {
		// 新建配置
		SegmenterConfig config = new SegmenterConfig();
		// 开启人名匹配
		config.setICnName(true);
		// 追加同义词
		config.setAppendCJKSyn(true);
		// 创建分词器，使用默认单例字典
		return new JcsegAnalyzer(ISegment.COMPLEX, config, DictionaryFactory.createSingletonDictionary(config));
	}

	/**
	 * 测试进行分词
	 *
	 * @param analyzer 传入分词器
	 * @param text     测试文本
	 * @return 返回分词后词组
	 */
	public static String[] tokenize(Analyzer analyzer, String text) {
		List<String> result = new ArrayList<>();
		try (TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text))) {
			CharTermAttribute attr = tokenStream.addAttribute(CharTermAttribute.class);
			tokenStream.reset();
			while (tokenStream.incrementToken()) {
				result.add(attr.toString());
			}
			tokenStream.end();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toArray(new String[0]);
	}

}